package com.security.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

//import com.tencent.bugly.crashreport.CrashReport;

import java.lang.reflect.Member;
import java.util.concurrent.CountDownLatch;

/**
 * @author : YOULU  sym
 * date   : 2020/6/12 0012
 * desc   :
 */
public class SecurityUtils {

    private static final String TAG = "YouluUtils";

    private static boolean DEBUG = true;

    static {
        System.loadLibrary("native-lib");
    }

    private static class Holder {
        private static SecurityUtils sInstance = new SecurityUtils();
    }

    private SecurityUtils() {
    }

    public static SecurityUtils getInstance() {
        return Holder.sInstance;
    }

    public native byte[] test(String str) throws Throwable;

    public native void check(Context context) throws Throwable;

//    public native String dbb(String password, byte[] content, String charset, String model, byte[] iv) throws Throwable;

    //String decrypt(String password, String content, String model, byte[] iv)
//    public native String d(String password, String content, String model, byte[] iv) throws Throwable;

//    public native byte[] etb(String password, String content, String charset, String model, byte[] iv) throws Throwable;

    //public static String encrypt(String password, String content, String model, byte[] iv)
//    public native String e(String password, String content, String model, byte[] iv) throws Throwable;

//    public native String re(String data, String keySrc) throws Throwable;

    public native void printMemberName(Member member);

    public void registerUsbReceiver(Context context) {
        context.getClass().getName();
        String ACTION_USB_PERMISSION = "android.hardware.usb.action.USB_STATE";
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_USB_PERMISSION);
        context.registerReceiver(mUsbReceiver, filter);
    }

    public void unregisterUsbReceiver(Context context) {
        context.unregisterReceiver(mUsbReceiver);
    }

    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (DEBUG) {
                try {
                    if (intent.getBooleanExtra("connected", false)) {
                        check(context);
                    }
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        }
    };

    public void reportRemote(int type) {

        Log.e(TAG, "reportRemote is called!  thread name:: " + Thread.currentThread().getName());
        CountDownLatch countDownLatch = new CountDownLatch(1);
        new Thread(() -> {
            try {
                Log.e(TAG, "CrashReport is called!");
//                CrashReport.postCatchedException(new YouluSecurityException(String.valueOf(type)));
            } catch (Throwable e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
        }).start();
        try {
            countDownLatch.await();
            Log.e(TAG, "reportRemote is called! main thread");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private class YouluSecurityException extends Throwable {

        public YouluSecurityException(String msg) {
            super(msg);
        }
    }
}
